/*
 *  Copyright 2010 UPF Telecos 4th group G.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package edu.upf.content;

/**
 * Class used to convert and get a serializable Image for sending thumbnails
 * @author netjois
 */
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.MemoryImageSource;
import java.awt.image.PixelGrabber;
import java.io.Serializable;

public class SerializeImage implements Serializable {

    /**
     * Gets serializable int array from a Image
     * @param img
     * @param width
     * @param height
     * @return
     * @throws InterruptedException
     */
    public int[] getArrayFromImage(Image img, int width, int height) throws
            InterruptedException {
        int[] pixels = new int[width * height];
        PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, pixels,
                0, width);
        pg.grabPixels();
        return pixels;
    }
   
    /**
     * Gets an Image object from a array of pixels
     * @param pixels
     * @param width
     * @param height
     * @return
     */
    public Image getImageFromArray(int[] pixels, int width, int height) {
        MemoryImageSource mis = new MemoryImageSource(width, height, pixels, 0,
                width);
        Toolkit tk = Toolkit.getDefaultToolkit();
        return tk.createImage(mis);
    }  //  private Image getImageFromArray()
}
